<?php

namespace Index\Controller;

class ArticleController extends CommonController {
	
	// 文章分类列表
	public function cateList() {
		$guid = $_SESSION ['guid'];
		$cate = M ( 'cate' );
		// $_GET['p']=1;
		
		$count = $cate->where ( array (
				'guid' => $guid 
		) )->count (); // 查询满足要求的总记录数
		$Page = new \Think\Page ( $count, 5 ); // 实例化分页类 传入总记录数和每页显示的记录数
		
		$data = $cate->where ( array (
				'guid' => $guid 
		) )->order ( 'sort' )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		$Page->setConfig ( 'header', '<li><a>共 %TOTAL_ROW% 条记录</a></li>' );
		$Page->setConfig ( 'prev', '上一页' );
		$Page->setConfig ( 'next', '下一页' );
		$Page->setConfig ( 'first', '第一页' );
		$Page->setConfig ( 'theme', "%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%" );
		
		$show = $Page->show (); // 分页显示输出
		$this->assign ( 'page', $show ); // 赋值分页输出
		$this->cate = $data;
		$this->display ();
	}
	
	// 添加分类视图
	public function addCate() {
		$this->display ();
	}
	// 处理添加分类视图
	public function doAddCate() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		} else {
			$cate_name = $_POST ['cate_name'];
			$data = array (
					'cate_name' => $cate_name,
					'sort' => $_POST ['sort'],
					'time' => date ( 'Y-m-d H:i:s' ),
					'user' => $_SESSION ['username'],
					'guid' => $_SESSION ['guid'] 
			);
			if ($this->isSame ( 'cate_name', $cate_name, 'cate', $_SESSION ['guid'] )) {
				if (M ( 'cate' )->add ( $data )) {
					$this->success ( '添加成功', U ( 'cateList' ) );
				} else {
					$this->error ( '添加失败', U ( 'addCate' ) );
				}
			} else {
				$this->error ( '添加失败，已经有此分类！', U ( 'addCate' ) );
			}
		}
	}
	// 修改分类视图
	public function editCate() {
		$id = I ( 'get.cid', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '修改分类信息失败！' );
		}
		
		$sql = M ( 'cate' );
		$where = array (
				'cid' => $id,
				'guid' => $_SESSION ['guid'] 
		);
		
		$cate = $sql->where ( $where )->find ();
		
		$this->assign ( 'cate', $cate );
		
		$this->display ();
	}
	// 处理修改分类
	public function doEditCate() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		}
		$guid=$_SESSION['guid'];
		$cate = M ( 'cate' );
		
		$where = array (
				'cid' => $_POST['cid'],
				'guid'	=>$guid 
		);
		$data = array(
			'guid'		=>	$guid,
			'cid'		=>	$_POST['cid'],
			'cate_name'	=>	$_POST['cate_name'],
			'sort'		=>	$_POST['sort'],
			'edit_time'		=>	date('Y-m-d H:i:s')
		);
		$res = $cate->where ( $where )->save ($data);
		//echo $cate->_sql ();
		
		if ($res) {
			$this->success ( '分类修改成功！', U ( 'catelist' ) );
		} else {
			$this->error ( '修改失败！', U ( 'editCate' ) );
		}
	}

	/**
	 * 处理删除分类
	 */
	public function delCate() {
		if (! IS_GET) {
			$this->error ( '页面不存在！' );
		}
		$id = I ( 'get.cid', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '删除分类信息失败！' );
		}
	
		$cate = M ( 'cate' );
		if ($cate->where ( array (
				'cid' => $id,
				'guid' => $_SESSION ['guid']
		) )->delete ()) {
			$this->redirect ( '/Article/cateList' );
		} else {
			$this->error ( '删除失败', U ( '/Article/cateList' ) );
		}
	}
	
	/**
	 * 文章列表显示
	 */
	public function artList() {
		header ( 'Content-type: text/html; charset=utf-8' );
		$guid = $_SESSION ['guid'];
		$sql = M ( 'article' );
		
		$count = $sql->where ( array (
				'guid' => $guid 
		) )->count (); // 查询满足要求的总记录数
		$Page = new \Think\Page ( $count, 5 ); // 实例化分页类 传入总记录数和每页显示的记录数
		
		$cateArr = M ( 'cate' )->where ( array (
				'guid' => $guid 
		) )->select ();
		// 把分类转换为二维数组 array('cid'=>'cate_name')
		$cate = array ();
		for($i = 0; $i < count ( $cateArr ); $i ++) {
			$cate [$cateArr [$i] ['cid']] = $cateArr [$i] ['cate_name'];
		}
		
		$data = $sql->where ( array (
				'guid' => $guid 
		) )->order ( 'sort' )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		// 把$data中的分类cid转化为对应分类中的cate_name
		for($i = 0; $i < count ( $data ); $i ++) {
			$data [$i] ['cid'] = $cate [$data [$i] ['cid']];
		}
		
		// $this->assign('cid',$cid);
		$Page->setConfig ( 'header', '<li><a>共 %TOTAL_ROW% 条记录</a></li>' );
		$Page->setConfig ( 'prev', '上一页' );
		$Page->setConfig ( 'next', '下一页' );
		$Page->setConfig ( 'first', '第一页' );
		$Page->setConfig ( 'theme', "%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%" );
		
		$show = $Page->show (); // 分页显示输出
		$this->assign ( 'page', $show ); // 赋值分页输出
		$this->assign ( 'art', $data ); // 赋值分页输出
		                            // $this->art = $data;
		$this->display ();
	}
	
	// 添加文章视图
	public function addArt() {
		$guid = $_SESSION ['guid'];
		$cate = M ( 'cate' );
		$data = $cate->where ( array (
				'guid' => $guid 
		) )->order ( 'sort' )->select ();
		$this->cate = $data;
		
		$this->display ();
	}
	// 处理添加文章
	public function doAddArt() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		} else {
			$title = $_POST ['title'];
			$data = array (
					'title' => $_POST ['title'],
					'cid' => $_POST ['cate'],
					'content' => $_POST ['content'],
					'description' => $_POST ['description'],
					'sort' => $_POST ['sort'],
					'keywords' => $_POST ['keywords'],
					'add_time' => date ( 'Y-m-d H:i:s' ),
					'user' => $_SESSION ['username'],
					'guid' => $_SESSION ['guid'] 
			);
			if (M ( 'article' )->add ( $data )) {
				$this->success ( '添加成功', U ( 'artList' ) );
			} else {
				$this->error ( '添加失败', U ( 'addArt' ) );
			}
		}
	}
	
	// 处理删除文章
	public function delArt() {
		if (! IS_GET) {
			$this->error ( '页面不存在！' );
		}
		// if ($this->isAdmin ()) {
		$id = I ( 'get.aid', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '删除文章失败！' );
		}
		
		$cate = M ( 'article' );
		if ($cate->where ( array (
				'aid' => $id,
				'guid' => $_SESSION ['guid'] 
		) )->delete ()) {
			$this->redirect ( '/Article/artList' );
		} else {
			$this->error ( '删除失败', U ( '/Article/artList' ) );
		}
	}

	/**
	 * 修改文章视图
	 */
	public function editArt() {
		$id = I ( 'get.aid', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '修改文章失败！' );
		}
			
		$guid = $_SESSION ['guid'];
		$cate = M ( 'cate' );
		$data = $cate->where ( array (
				'guid' => $guid
		) )->order ( 'sort' )->select ();
		
		$sql = M ( 'article' );
		$where = array (
				'aid' => $id,
				'guid' => $_SESSION ['guid']
		);
	
		$art = $sql->where ( $where )->find ();

		$this->cate = $data;
		$this->assign ( 'art', $art );
	
		$this->display ();
	}
	
	/**
	 * 处理修改文章
	 */
	public function doEditArt() {
	if (! IS_POST) {
			$this->error ( '页面不存在！' );
		}
		$guid=$_SESSION['guid'];
		$art = M ( 'article' );
		
		$where = array (
				'aid' => $_POST['aid'],
				'guid'	=>$guid 
		);
		$data = array(
			'title'	=>	$_POST['title'],
			'cid'	=>	$_POST['cate'],
			'content'	=>	$_POST['content'],
			'sort'		=>	$_POST['sort'],
			'keywords'		=>	$_POST['keywords'],
			'description'		=>	$_POST['description'],
			'edit_time'		=>	date('Y-m-d H:i:s')
		);
		$res = $art->where ( $where )->save ($data);
		//echo $cate->_sql ();
		
		if ($res) {
			$this->success ( '文章修改成功！', U ( 'artList' ) );
		} else {
			$this->error ( '修改失败！', U ( 'editArt' ) );
		}
	}
}